<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true">添加分类</el-button>
      </div>
      <el-table :data="CateList" style="width: 100%" stripe border>
        <!-- porp 指定要渲染的属性名 -->
        <el-table-column label="序号" type="index" width="50"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scoped">
            <el-button type="primary" size="mini" @click="showEditDialog(scoped.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="hDel(scoped.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  <!-- 添加分类盒子 -->
   <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      :close-on-click-modal='false'
      @closed="$refs.addForm.resetFields()"
      width="35%">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="hAdd">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="修改文章分类"
      :visible.sync="editVisible"
      :close-on-click-modal='false'
      @closed="$refs.editForm.resetFields()"
      width="35%">
      <!-- 添加的表单 -->
      <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="hEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      CateList: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadCateList()
  },
  methods: {
    async loadCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      this.CateList = res.data
      // console.log(this.CateList)
    },
    hAdd() {
      this.$refs.addForm.validate(async valid => {
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.addVisible = false
        this.loadCateList()
      })
    },
    showEditDialog(row) {
      if (row.id === 1 || row.id === 2) return this.$message.warning('管理员不允许修改前两条数据')
      this.editForm = { ...row }
      this.editVisible = true
    },
    hEdit() {
      this.$refs.editForm.validate(async valid => {
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.editVisible = false
        this.loadCateList()
      })
    },
    // 删除模块
    hDel(id) {
      // 根据传入 id 限制不允许删除内容
      if (id === 1 || id === 2) return this.$message.warning('不允许删除该粉类')
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发送请求根据 id 删除
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        // 根据请求结果做出判断提示
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新渲染
        this.loadCateList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
